# -*- coding: utf-8 -*-
########## INFO #############
#
# program: profit-economy
#    file: profit.py
#  python: version 2.6 is required
#     use: python -i profit.py
#   autor: Alexander Anikeev
# version: 0.2
#  update: 20.04.11
#

VAT = 18
PROFITS_TAX = 20
PROPERTY_TAX = 20

class Profit():
    def __init__(self):
        self.profit_pretax = 0.0
        self.profit_fortax = 0.0
        self.profit_clear = 0.0
        self.profits_tax =0.0
        self.profitability_of_resourses = 0.0
        self.profitability_of_costly = 0.0
        self.ma = self.MainActivities()
        self.sop = self.SaleOfProperty()
        self.oa = self.OtherActivities()
    def _calculate(self):
        self.profit_pretax = self.ma.profit + self.sop.profit + self.oa.profit
        self.profit_fortax = self.profit_pretax - self.oa.profit_taxfree + self.oa.expenses_above
        self.profits_tax = (self.profit_fortax * PROFITS_TAX) / 100
        self.profit_clear = self.profit_pretax - self.profits_tax
    def clear_ma(self):
        self.ma.revenue_with_vat = 0.0
        self.ma.expenses = 0.0
        self.ma.revenue_vat = 0.0
        self.ma.revenue_without_vat = 0.0
        self.ma.profit = 0.0
        self._calculate()
    def clear_sop(self):
        self.sop.revenue_with_vat = 0.0
        self.sop.residual_value = 0.0
        self.sop.revenue_vat = 0.0
        self.sop.revenue_without_vat = 0.0
        self.sop.profit = 0.0
        self._calculate()
    def clear_oa(self):
        self.oa.revenue = 0.0
        self.oa.expenses = 0.0
        self.oa.profit = 0.0
        self.oa.profit_taxfree = 0.0
        self.oa.expenses_above = 0.0
        self._calculate()
        
    class MainActivities():
        def __init__(self):
            self.revenue_with_vat = 0.0
            self.expenses = 0.0
            self.revenue_vat = 0.0
            self.revenue_without_vat = 0.0
            self.profit = 0.0
        def add_revenue_with_vat(self, rev_with_vat):
            self.revenue_with_vat += rev_with_vat
            self._calculate()
        def add_expense(self, exps):
            self.expenses += exps
            self._calculate()
        def _calculate(self):
            self.revenue_vat = (self.revenue_with_vat * VAT) / (VAT+ 100)
            self.revenue_without_vat = self.revenue_with_vat - self.revenue_vat
            self.profit = self.revenue_without_vat - self.expenses
            profits._calculate()
    
    class SaleOfProperty():
        def __init__(self):
            self.revenue_with_vat = 0.0
            self.residual_value = 0.0
            self.revenue_vat = 0.0
            self.revenue_without_vat = 0.0
            self.profit = 0.0
        def add_revenue_with_vat(self, rev_with_vat):
            self.revenue_with_vat += rev_with_vat
            self._calculate()
        def add_residual_value(self, res_val):
            self.residual_value += res_val
            self._calculate()
        def _calculate(self):
            self.revenue_vat = (self.revenue_with_vat*VAT)/(VAT+100)
            self.revenue_without_vat = self.revenue_with_vat - self.revenue_vat
            self.profit = self.revenue_without_vat - self.residual_value
            profits._calculate()
            
    class OtherActivities():
        def __init__(self):
            self.revenue = 0.0
            self.expenses = 0.0
            self.profit = 0.0
            self.profit_taxfree = 0.0
            self.expenses_above = 0.0
        def add_revenue(self, rev):
            self.revenue += rev
            self._calculate()
        def add_expense(self, exps):
            self.expenses += exps
            self._calculate()
        def add_taxfree(self, profit):
            self.profit_taxfree += profit
        def _calculate(self):
            self.profit = self.revenue +self.profit_taxfree - self.expenses
            profits._calculate()
profits = Profit()

class ProductsAndServices():
    def __init__(self):
        self.pos = []
    class ProductOrService():
        def __init__(self):
            self.name = ""
            self.revenue_with_vat = None
            self.expense = None
            self.revenue_without_vat = None
            self.volume = None
            self.price_with_vat = None
            self.price_without_vat = None
            self.cost = None
            self.profitability = None
            self._complete = False
        def add_name(self, name):
            self.name = name
        def add_revenue_with_vat(self, rev_with_vat):
            self.revenue_with_vat = rev_with_vat
            if self._complete == False:
                self._check()
        def add_expense(self, exps):
            self.expense = exps
            if self._complete == False:
                self._check()
        def add_revenue_without_vat(self, rev_without_vat):
            self.revenue_without_vat = rev_without_vat
            if self._complete == False:
                self._check()
        def add_volume(self, vol):
            self.volume = vol
            if self._complete == False:
                self._check()
        def add_price_with_vat(self, prc_with_vat):
            self.price_with_vat = prc_with_vat
            if self._complete == False:
                self._check()
        def add_price_without_vat(self, prc_wthout_vat):
            self.price_without_vat = prc_wthout_vat
            if self._complete == False:
                self._check()
        def add_cost(self, cost):
            self.cost = cost
            if self._complete == False:
                self._check()
        def add_profitability(self, pa):
            self.profitability = pa
            if self._complete == False:
                self._check()
        def _check(self):
            if self.revenue_with_vat == None or self.expense == None:
                if self.revenue_with_vat == None:
                    try:
                        if self.revenue_without_vat == None:
                            if self.price_with_vat == None:
                                if self.price_without_vat == None:
                                    self.price_without_vat = self.cost * (1 + self.profitability / 100)
                                self.price_with_vat = self.price_without_vat * (1 + VAT / 100)
                            self.revenue_with_vat = self.price_with_vat * self.volume
                        else:
                            self.revenue_with_vat = self.revenue_without_vat * (1 + VAT / 100)
                    except TypeError: pass
                if self.expense == None:
                    try:
                        if self.cost == None:
                            if self.price_without_vat == None:
                                if self.price_with_vat == None:
                                    self.price_with_vat = self.revenue_with_vat / self.volume
                                self.price_without_vat = self.price_with_vat / (1 + VAT / 100)
                            self.cost = self.price_without_vat / (1 + self.profitability / 100)
                        self.expense = self.cost * self.volume
                    except TypeError: pass
            if self.revenue_with_vat != None and self.expense != None:
                profits.ma.add_revenue_with_vat(self.revenue_with_vat)
                profits.ma.add_expense(self.expense)
                self._complete = True
pas = ProductsAndServices()

class Expenditures():
    def __init__(self):
        self.exp_list = []
    class Expenditure():
        def __init__(self):
            self.name = ""
            self.type = None
            self.expense = None
        def add_expense(self, exps):
            self.expense = exps
            self._check()
        def add_type(self, type):
            self.type = type
            self._check()
        def add_name(self, name):
            self.name = name
        def _check(self):
            if self.type != None and self.expense != None:
                if self.type == "Current":
                    profits.ma.add_expense(self.expense)
                if self.type == "Other":
                    profits.oa.add_expense(self.expense)
    class Payment(Expenditure):
        def __init__(self):
            self.name = "Оплата труда"
            self.type = "Current"
            self.expense = None
            self.wage = None
            self.number = None
            self.bonus = None
            self.district_rate = None
            self.sctax = None
            self.tax = None
        def add_wage(self, wage):
            self.wage = wage
            self._calculate()
        def add_number(self, nmb):
            self.number = nmb
            self._calculate()
        def add_bonus(self, bns):
            self.bonus = bns
            self._culculate()
        def add_district_rate(self, dr):
            self.district_rate = dr
            self._calculate()
        def add_tax(self, tax):
            self.tax = tax
            self._calculate()
        def _calculate(self):
            if self.expense == None and self.wage != None and self.number != None and self.bonus != None and self.district_rate != None and self.tax != None:
                self.expense = self.wage * self.number * (1 + self.bonus / 100 + self.district_rate / 100) * 12
                self.sctax = self.expense * (self.tax / 100)
                profits.ma.add_expense(self.expense)
                profits.ma.add_expense(self.sctax)
    class Amortization(Expenditure):
        def __init__(self):
            self.name = "Аммортизация"
            self.type = "Current"
            self.expense = None
            self.funds = None
            self.norm = None
        def add_funds(self, funds):
            self.funds = funds
            self._calculate()
        def add_norm(self, norm):
            self.norm = norm
            self._calculate()
        def _calculate(self):
            if self.expense == None and self.funds != None and self.norm != None:
                self.expense = self.funds * (self.norm / 100)
                self._check()
    class Energy(Expenditure):
        def __init__(self):
            self.name = "Электроэнергия"
            self.type = "Current"
            self.expense = None
            self.use = None
            self.rate = None
        def add_use(self, use):
            self.use = use
            self._calculate()
        def add_rate(self, rate):
            self.rate = rate
            self._calculate()
        def _calculate(self):
            if self.expense == None and self.rate != None and self.use != None:
                self.expense = self.rate * self.use
                self._check()
    class Rent(Expenditure):
        def __init__(self):
            self.name = "Аренда"
            self.type = "Current"
            self.expense = None
            self.size = None
            self.rate = None
        def add_size(self, size):
            self.size = size
            self._calculate()
        def add_rate(self, rate):
            self.rate = rate
            self._calculate()
        def _calculate(self):
            if self.expense == None and self.rate != None and self.size != None:
                self.expense = self.rate * self.size * 12
                self._check()
    class Property_tax(Expenditure):
        def __init__(self):
            self.name = "Налог на имущество"
            self.type = "Other"
            self.expense = None
            self.volume = None
        def add_volume(self, vol):
            self.volume = vol
            self.expense = self.volume * (PROPERTY_TAX / 100)
            self._check()
expenditures = Expenditures()

class PropertyForSale():
    def __init__(self):
        prop_list = []
    class Property():
        def __init__(self):
            self.name = ""
            self.cost = None
            self.amort_norm = None
            self.amort_year =None
            self.amort = None
            self.usage = None
            self.residual = None
            self.revenue = None
            self.get = None
        def add_get(self, g):
            self.get = g
            self._calculate()
        def add_cost(self, c):
            self.cost = c
            self._calculate()
        def add_amort_norm(self, norm):
            self.norm = norm
            self._calculate()
        def add_amort_year(self, ay):
            self.amort_year = ay
            self._calculate()
        def add_amort(self, a):
            self.amort = a
            self._calculate()
        def add_usage(self, u):
            self.usage = u
            self._calculate()
        def add_residual(self, r):
            self.residual = r
            self._check()
        def add_revenue(self, rev):
            self.revenue = rev
            self._check()
        def _calculate(self):
            if self.revenue == None:
                try:
                    if self.residual == None:
                        if self.amort == None:
                            if self.amort_year == None:
                                self.amort_year = (self.cost * self.amort_norm) / 100
                            self.amort = self.amort_year * self.usage
                        self.residual = self.cost - self.amort
                    self.revenue = self.residual * self.get
                except TypeError: pass
            if self.residual == None:
                try:
                    self.residual = self.revenue / self.get
                except TypeError: pass
            self._check()
        def _check(self):
            if self.revenue != None and self.residual != None:
                profits.sop.add_revenue_with_vat(self.revenue)
                profits.sop.add_residual_value(self.residual)

